<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//模块管理
class Module extends MY_Controller {

	function __construct(){
		
		parent::__construct();
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$this->load->model('Index/ModuleModel','Modules');
		$arr = $this->Modules->get_list();
		
		$result = array(
			'list'=>$arr,
			'count'=>count($arr)
		);
		
		$this->data_out($result);
	}
	
	
	//start
	/**
	* @name 获取模块菜单列表
	* @return 
	*/
	//end
	function get_menu_list(){
		$user_id = $this->user_id;
		
		$this->load->model('Index/ModuleModel','Modules');
		$list = $this->Modules->get_list(array('is_install'=>1,'is_enable'=>1));
		
		$arr = array();
		foreach($list as $val){
			$mpath = $val['path'].'/Admin/menu.php';
			if(!file_exists($mpath)){
				continue;
			}
			$ls = @include($mpath);
			if($ls && is_array($ls)){
				foreach($ls as $item){
					$arr[] = $item;
				}
				
			}
		}
		
		$this->data_out($arr);
	}
	
	//start
	/**
	* @name 安装模块
	* @param name ture char 名称
	* @return 
	*/
	//end
	function install(){
		$name = $this->input->post('name',null,'');
		
		$this->load->model('Index/ModuleModel','Modules');
		
		$info = $this->Modules->get_module($name);
		if(!$info){
			$this->err_out("模块【{$name}】不存在");
		}
		
		$is_exist = $this->Modules->_where_exist(array('module_name'=>$name));
		if($is_exist){
			$this->err_out("当前模块已安装");
		}
		
		$database_path = $info['path']."/database.php";
		if(file_exists($database_path)){
			$ls_arr = include_once($database_path);
			if(isset($ls_arr['install']) && !empty($ls_arr['install'])){
				$sqls = explode(';',$ls_arr['install']);
				
				foreach($sqls as $sql){
					$sql = trim($sql);
					if(!empty($sql)){
						$this->Modules->db->query($sql);
					}
				}
			}
		}
		
		$arr = array(
			'name'=>$info['name'],
			'module_name'=>$info['module_name'],
			'info'=>$info['info'],
			'is_enable'=>1
		);
		$this->Modules->add($arr);
		$this->add_log("安装模块【{$name}】",'module');
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 卸载模块
	* @param name ture char 名称
	* @return 
	*/
	//end
	function uninstall(){
		$name = $this->input->post('name',null,'');
		$this->load->model('Index/ModuleModel','Modules');
		
		$row = $this->Modules->field_get_info($name,'module_name');
		if(!$row){
			$this->err_out("模块【{$name}】未安装");
		}
		
		$info = $this->Modules->get_module($name);
		if($info){
			$database_path = $info['path']."/database.php";
			if(file_exists($database_path)){
				$ls_arr = include_once($database_path);
				if(isset($ls_arr['uninstall']) && !empty($ls_arr['uninstall'])){
					$sqls = explode(';',$ls_arr['uninstall']);
					
					foreach($sqls as $sql){
						$sql = trim($sql);
						if(!empty($sql)){
							$this->Modules->db->query($sql);
						}
					}
				}
			}
		}
		
		$this->Modules->del("id='{$row['id']}'");
		
		$this->add_log("卸载模块【{$name}】",'module');
		$this->data_out(1);
	}
	
	
	
}
